package com.hornsun.manage.order.api;

import com.hornsun.client.personalPage.dto.MyOrderDetailDTO;
import com.hornsun.client.personalPage.service.OrderService;
import com.hornsun.domain.constant.types.OrderType;
import com.hornsun.domain.dbo.OrderForm;
import com.hornsun.manage.order.dto.OrderDTO;
import com.hornsun.repository.OrderFormRepository;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 编译中... on 2018/5/16.
 */
@RestController
@RequestMapping("/manage/order")
public class OrderApi {

    @Resource
    private OrderService orderService;

    @Resource
    private OrderFormRepository orderFormRepository;

    /**
     * 分页获取所有订单信息
     * @param page
     * @return
     */
    @GetMapping("/getOrders")
    public ResponseEntity<OrderDTO> getOrders(@RequestParam("page")int page){
        List<OrderForm> list = orderFormRepository.getOrders(page);
        List<MyOrderDetailDTO> orderDetailDTOList = orderService.queryOrderDetail(list);
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setMyOrderDetailDTOList(orderDetailDTOList);
        orderDTO.setCount(orderFormRepository.getTotalCount());
        return new ResponseEntity<OrderDTO>(orderDTO, HttpStatus.OK);
    }

    /**
     * 分页获取各订单状态的订单信息
     * @param type
     * @param page
     * @return
     */
    @GetMapping("/getOrdersByType")
    public ResponseEntity<OrderDTO> getOrdersByType(@RequestParam("type")int type, @RequestParam("page")int page){
        List<OrderForm> list = orderFormRepository.getOrdersByType(type,page);
        List<MyOrderDetailDTO> orderDetailDTOList = orderService.queryOrderDetail(list);
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setMyOrderDetailDTOList(orderDetailDTOList);
        orderDTO.setCount(orderFormRepository.getCountByType(type));
        return new ResponseEntity<OrderDTO>(orderDTO,HttpStatus.OK);
    }


    /**
     *确认发货
     * @param orderGuid
     * @return
     */
    @Transactional
    @PostMapping("/deliverGoods")
    public ResponseEntity<?> deliverGoods(@RequestParam("guid")String orderGuid ,@RequestParam("waybill")String waybill){
        int status = OrderType.DELIVER_GOODS.getType();
        OrderForm orderForm = new OrderForm();
        orderForm.setOrderGuid(orderGuid);
        orderForm.setWaybill(waybill);
        orderForm.setTransactionStatus((byte)status);
        orderFormRepository.updateOrderForm(orderForm);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }

    /**
     * 已收货
     * @param orderGuid
     * @return
     */
    @Transactional
    @PostMapping("/receiveGoods")
    public ResponseEntity<?> receiveGoods(@RequestParam("guid")String orderGuid){
        int status = OrderType.FINISH.getType();
        OrderForm orderForm = new OrderForm();
        orderForm.setOrderGuid(orderGuid);
        orderForm.setTransactionStatus((byte)status);
        orderFormRepository.updateOrderForm(orderForm);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }


    /**
     * 订单搜索（通过订单号）
     * @param alias
     * @return
     */
    @GetMapping("/getOrderByAlias")
    public ResponseEntity<?> getOrderByAlias(@RequestParam("alias")String alias){
        List<OrderForm> orderFormList = orderFormRepository.getByAlias(alias.replace(" ",""));
        List<MyOrderDetailDTO> orderDetailDTOList = orderService.queryOrderDetail(orderFormList);
        return new ResponseEntity<Object>(orderDetailDTOList,HttpStatus.OK);
    }


}
